/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package objects;

import graphic.Color;
import java.util.HashMap;
import graphic.Graphic;
import org.lwjgl.util.vector.Vector3f;

/**
 *
 * @author _Bartek
 */
public class Font {
    private HashMap <Character,OBJ_Loader.Object> alphabet = new HashMap<Character,OBJ_Loader.Object>();
    private Color color;
    private Vector3f rot = new Vector3f(0,0,0);
    private Vector3f pos = new Vector3f(0,0,0);
    private float scale = 1.0f;
    
    
    public Font(){
        Character tmp = 'a';
        while(true){
            if(tmp=='z'+ 1) break;
             OBJ_Loader.Object letterTmp = new OBJ_Loader.Object("obj/letters/"+tmp+".obj",0.0f,0.0f,0.0f);
             alphabet.put(tmp, letterTmp);
            tmp++;
        } 
        Character[] additionalTmp = new Character[5];
        additionalTmp[0] = '(';
        additionalTmp[1] = '!';
        additionalTmp[2] = ')';
        
        for(int i=0;i<3;i++){
            OBJ_Loader.Object letterTmp = new OBJ_Loader.Object("obj/letters/"+additionalTmp[i]+".obj",0.0f,0.0f,0.0f);
             alphabet.put(additionalTmp[i], letterTmp);
        }
        
        OBJ_Loader.Object letterTmp = new OBJ_Loader.Object("obj/letters/zap.obj",0.0f,0.0f,0.0f);
        alphabet.put('?', letterTmp);
        letterTmp = new OBJ_Loader.Object("obj/letters/dwu.obj",0.0f,0.0f,0.0f);
        alphabet.put(':', letterTmp);
        letterTmp = new OBJ_Loader.Object("obj/letters/kro.obj",0.0f,0.0f,0.0f);
        alphabet.put('.', letterTmp);
        pos.x = 0.0f; pos.y = 0.0f; pos.z = 0.0f;
        rot.x = 0.0f; rot.y = 0.0f; rot.z = 0.0f;
    }
    public void WriteLn(String text){
        Graphic.e.setColor(color);
        WriteLine(text, pos.x, pos.y, pos.z, scale, rot.x, rot.y, rot.z);
    }
    public void WriteLn(String text, float x, float y, float z){
        Graphic.e.setColor(color);
        WriteLine(text, x, y, z, this.scale, rot.x, rot.y, rot.z);
    }
    public void WriteLn(String text, float x, float y, float z, float scale){
        Graphic.e.setColor(color);
        WriteLine(text, x, y, z, scale, rot.x, rot.y, rot.z);
    }
    public void WriteLn(String text, float x, float y, float z, float scale, Color color){
        Graphic.e.setColor(color);
        WriteLine(text, x, y, z, scale, rot.x, rot.y, rot.z);
    }
    public void WriteLn(String text, float x, float y, float z, float scale, Color color, float rotx, float roty, float rotz){
        Graphic.e.setColor(color);
        WriteLine(text, x, y, z, scale, rotx, roty, rotz);
    }
    private void WriteLine(String text, float x, float y, float z, float scale, float rx, float ry, float rz){
        Graphic.e.push();
        Graphic.e.translate3f(x, y, z);
        Graphic.e.rotX(rx);
        Graphic.e.rotY(ry);
        Graphic.e.rotZ(rz);
        //tmp
        text.toLowerCase();
        float whitespace = 0;
        float previousVal = 0;
        for(int i=0;i<text.length();i++){
            if(text.charAt(i)==' ') {
                whitespace+=1f*scale;
                continue;
            }
            previousVal+=whitespace;
            alphabet.get(text.charAt(i)).move(previousVal, 0, 0);
            alphabet.get(text.charAt(i)).setScale(scale);
            alphabet.get(text.charAt(i)).draw();
            
            switch(text.charAt(i)){
                case 'f':
                case 'i':
                case 'j':
                case 'l':
                case '?':
                case '!':
                case '.':
                case ':':
                case '(':
                case ')':
                     whitespace = 0.3f*scale;
                    
                    break;
                case 'w':
                case 'm':
                         whitespace = 0.8f*scale;
                        break;
                default:
                    whitespace = 0.5f*scale;
                    break;
            }
        }
        Graphic.e.pop();

    }
    public void setRotate(float x,float y, float z){
        rot.x=x;
        rot.y=y;
        rot.z=z;
    }
    public void setRotateX(float ang){
        rot.x=ang;
    }
    public void setRotateY(float ang){
        rot.y=ang;
    }
    public void setRotateZ(float ang){
        rot.z=ang;
    }
    public void addRotateX(float ang){
        rot.x+=ang;
    }
    public void addRotateY(float ang){
        rot.y+=ang;
    }
    public void addRotateZ(float ang){
        rot.z+=ang;
    }
    
    public void setScale(float s){
        scale=s;
    }
    public void addScale(float s){
        scale+=s;
    }
    public void setColor(float R, float G, float B,float A){
        
        color = new Color(R,G,B,A);
    }
    
    public void move(float x, float y, float z){
        pos.x=x;
        pos.y=y;
        pos.z=z;
    }
}
